package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据用户id，查询三条,eachLoveCount，loveCount，fanCount
    @Override
    public Map counts(Long id) {
        //查询互相喜欢（好友）
        Criteria criteria = Criteria.where("userId").is(id);
        Query query = Query.query(criteria);
        long eachLoveCount = mongoTemplate.count(query, Friend.class);

        //查询我喜欢的
        criteria.and("isLike").is(true);
        long loveCount = mongoTemplate.count(query, UserLike.class);

        //查询粉丝
        Criteria criteria1 = Criteria.where("likeUserId").is(id).and("isLike").is(true);
        Query query1 = Query.query(criteria1);
        long fanCount = mongoTemplate.count(query1, UserLike.class);

        //封装返回
        Map map = new HashMap();
        map.put("eachLoveCount", eachLoveCount);
        map.put("loveCount", loveCount);
        map.put("fanCount", fanCount);
        return map;
    }

    //根据用户id，分页查询喜欢
    @Override
    public List<UserLike> findLove(Long id, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(id).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize);
        return mongoTemplate.find(query, UserLike.class);
    }

    //根据用户id，分页查询粉丝
    @Override
    public List<UserLike> findNotLove(Long id, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("likeUserId").is(id).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize);
        return mongoTemplate.find(query, UserLike.class);
    }

    //根据用户id，关注用户,喜欢，互相喜欢是加为好友
    @Override
    public void saveFans(UserLike userLike) {
        Criteria criteria = Criteria.where("userId").is(userLike.getUserId()).and("likeUserId").is(userLike.getLikeUserId());
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        if (exists) {
            Update update = new Update();
            update.set("isLike", true);
            mongoTemplate.updateFirst(query, update, UserLike.class);
        } else {
            mongoTemplate.save(userLike);
        }
    }


    //是否互相喜欢
    @Override
    public Boolean huxiang(Long id, Long uid) {
        //查询数据库，我喜欢他么？
        Criteria criteria = Criteria.where("userId").is(id).and("likeUserId").is(uid);
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, UserLike.class);

        //他是否喜欢我
        Criteria criteria1 = Criteria.where("userId").is(uid).and("likeUserId").is(id);
        Query query1 = Query.query(criteria1);
        boolean exists1 = mongoTemplate.exists(query1, UserLike.class);
        //如果互相喜欢
        if (exists && exists1) {
            return true;
        } else {
            return false;
        }
    }

    //是否喜欢（新增接口）
    @Override
    public boolean alreadyLove(Long uid,Long id) {
        Query query = Query.query(Criteria.where("userId").is(id)
                .and("likeUserId").is(uid));
        UserLike one = mongoTemplate.findOne(query, UserLike.class);
        if (one != null) {
            return one.getIsLike();
        } else {
            return false;
        }

    }

    //左滑喜欢
    @Override
    public Boolean saveOrUpdate(Long userId ,Long likeUserId ,boolean isLike) {

        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、创建更新条件
                Update update = new Update();
                update.set("isLike" ,isLike);
                update.set("updated" ,System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //取消喜欢
    @Override
    public void delete(Long id, Long uid) {
        //查询数据库有这个数据么,删除
        Criteria criteria = Criteria.where("userId").is(id).and("likeUserId").is(uid);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, UserLike.class);

//        //我但方面不喜欢他，查询他喜不喜欢我
//        Criteria criteria1 = Criteria.where("userId").is(uid).and("likeUserId").is(id);
//        Query query1 = Query.query(criteria1);
//        boolean exists = mongoTemplate.exists(query1, UserLike.class);
//
//        //如果他喜欢我，修改它得字段为false
//        if (exists) {
//            Update update = new Update();
//            update.set("isLike", false);
//            mongoTemplate.updateFirst(query1, update, UserLike.class);
//        }
    }
    //右滑不喜欢

}

